PythonGUI:tkinter隐藏和“销毁”控件

您所在的位置:网站首页 tkinter frame切换 PythonGUI:tkinter隐藏和“销毁”控件

PythonGUI:tkinter隐藏和“销毁”控件

2023-09-20 16:56| 来源: 网络整理| 查看: 265

pack布局的情况下有pack_forget()方法让控件“不再显示”但控件还存在可以再次pack出来

实例,不难

from tkinter import * root = Tk() l1 = Label(root, text='pack_forget') b3 = Button(root, text='按钮') b1 = Button(root, text='隐藏', command=b3.pack_forget) b2 = Button(root, text='显示', command=b3.pack) l1.pack(fill=X) b1.pack(fill=X) b2.pack(fill=X) b3.pack() root.mainloop()

在这里插入图片描述 grid,place布局下也有对应的grid_forget(),place_forget()

这个控件只是不再显示,但依然存在 在内存里 !!

还有一个destroy(),但是这个是“销毁”,是无法重现的

除非控件不再用了,或者是想释放内存,否则不要destroy

from tkinter import * root = Tk() button_list = [] def add_button(): b = Button(root, text='按钮') b.pack(fill=X) button_list.append(b) def reduce_button(): if button_list: b = button_list.pop() # b.pack_forget() b.destroy() b1 = Button(root, text='添加按钮', command=add_button) b2 = Button(root, text='减少按钮', command=reduce_button) b1.pack() b2.pack() root.mainloop()

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3